package com.health.commons;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

	 static SendMail instance;
	  public static synchronized SendMail getInstance(){  
		         if(instance == null)  
		             instance = new SendMail();  
		         return instance;  
		     }  


	  /**
	   * �����ʼ�
	   * @param emailTitle ����
	   * @param emailContent ���� 
	   * @param toMail Ҫ���͵���
	 * @throws MessagingException 
	 * @throws AddressException 
	   */
	public void sendHtmlMail(String emailTitle,String emailContent,String toMail) throws AddressException, MessagingException
	{
		
		
		Properties props = new Properties();
		//��Ҫ���������smtp���ܣ����򱨴�javax.mail.AuthenticationFailedException: 454 Authentication failed, please open smtp flag first!
		props.setProperty("mail.smtp.host", "smtp.163.com");
		// ��������������
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.debug", "true");

		// Session������stmp��������Ϣ
		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						
						return new PasswordAuthentication("fkss2015@163.com",
								"!QAZ2wsx");
					}
				});

			MimeMessage msg = new MimeMessage(session);
		
		
 
			
			////com.sun.mail.smtp.SMTPSenderFailedException: 553 Mail from must equal authorized user
			//���ô��ĸ����䷢
			msg.setFrom(new InternetAddress("fkss2015@163.com"));
			
			//����Ҫ���͵�����
			msg.addRecipients(Message.RecipientType.TO,	toMail);
			//����
			msg.setSubject(emailTitle, "UTF-8");
			//ʱ��
			msg.setSentDate(new Date());
			//����
			msg.setText(emailContent, "UTF-8");

			// ʹ�þ�̬�ķ���������Ϣ
			// Transport.send(msg,new InternetAddress[]{new
			// InternetAddress("yybs76@qq.com")});
			Transport.send(msg);
	 
	
			
			//operResult=ResultCode.OPERATION_SUCCESS;
			//operStatus=ResultCode.OPERATION_STATUS_SUCCESS;
	
		
		
		
	}
}
